package xiyuan.localio;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import xiyuan.view.data.ViewInformation;
import xiyuan.view.data.WeiboUser;

public class ViewInfoReader {
	private ViewInformation viewinfo;
	public ViewInformation getViewInformation(){
		return viewinfo.clone();
	}
	public static void main(String[] args){
		try {
			new ViewInfoReader("大学",new String[]{"饮食","水饺","国防部"});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public ViewInfoReader(String searchTag,String[] tags) throws IOException {
		viewinfo = new ViewInformation();
		viewinfo.setSearchTag(searchTag);
		viewinfo.setAddiTags(tags);
		List<WeiboUser> userlist = new LinkedList<WeiboUser>();
		viewinfo.setUsers(userlist);
		int length = tags.length;
		File disFiles[] = new File[length + 1];
		disFiles[0] = new File("distance.txt");
		for (int i = 0; i < length; i++) {
			disFiles[i + 1] = new File("distance_" + tags[i] + ".txt");
		}
		Map<Long, WeiboUser> map = new HashMap<Long, WeiboUser>();
		for (int i = 0; i <= length; i++) {
			File disFile = disFiles[i];
			if (!disFile.exists())
				throw new FileNotFoundException();
			BufferedReader br = new BufferedReader(new FileReader(disFile));
			String str;
			while ((str = br.readLine()) != null) {
				String[] strs = str.split("\t");
				String uidS = strs[0];
				long uid;
				if (uidS.matches("\\d+"))
					uid = Long.parseLong(uidS);
				else
					continue;
				String username = strs[1];
				String disS = strs[2];
				double dis;
				if (disS.matches("-{0,1}\\d+\\.\\d+"))
					dis = Double.parseDouble(disS);
				else
					continue;
				if (map.containsKey(uid)){
					WeiboUser user = map.get(uid);
					user.getDistance()[i] = dis;
				}else{
					WeiboUser user = new WeiboUser(uid,length+1,3,true);
					userlist.add(user);
					user.getDistance()[i] = dis;
					user.setUsername(username);
				}
			}
		}

	}
}
